# 基础镜像
# https://hub.docker.com/_/python/tags?page=1&name=3.11-slim-bullseye
FROM docker.io/python:3.11-slim-bullseye

LABEL maintainer="myh"
#增加语言utf-8
ENV LANG=zh_CN.UTF-8
ENV LC_CTYPE=zh_CN.UTF-8
ENV LC_ALL=C
ENV PYTHONPATH=/data/InStock
EXPOSE 9988

# 使用国内镜像地址加速。修改debian apt更新地址，pip地址，设置时区
# https://opsx.alibaba.com/mirror
# https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
# 安装依赖库
# apt-get autoremove -y 删除没有用的依赖lib
# apt-get --purge remove 软件包名称 , 删除已安装包（不保留配置文件)

# 配置apt-get使用清华镜像源（国内访问更快更稳定）
RUN sed -i 's@http://deb.debian.org@https://mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list && \
    sed -i 's@http://security.debian.org@https://mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list && \
    sed -i 's@https://deb.debian.org@https://mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list && \
    sed -i 's@https://security.debian.org@https://mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list || true

# 配置pip使用清华镜像源（全局配置）
RUN mkdir -p /root/.pip && \
    echo "[global]" > /root/.pip/pip.conf && \
    echo "index-url = https://pypi.tuna.tsinghua.edu.cn/simple" >> /root/.pip/pip.conf && \
    echo "trusted-host = pypi.tuna.tsinghua.edu.cn" >> /root/.pip/pip.conf && \
    echo "[global]" > /etc/pip.conf && \
    echo "index-url = https://pypi.tuna.tsinghua.edu.cn/simple" >> /etc/pip.conf && \
    echo "trusted-host = pypi.tuna.tsinghua.edu.cn" >> /etc/pip.conf

# 设置时区并安装系统依赖
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone

# 更新apt源并安装依赖
RUN apt-get update && \
    apt-get install -y cron gcc make python3-dev default-libmysqlclient-dev build-essential pkg-config curl wget

# 安装Python依赖包（使用加速后的pip）
RUN pip install --no-cache-dir supervisor && \
    pip install --no-cache-dir mysqlclient && \
    pip install --no-cache-dir requests && \
    pip install --no-cache-dir arrow && \
    pip install --no-cache-dir numpy && \
    pip install --no-cache-dir SQLAlchemy && \
    pip install --no-cache-dir PyMySQL && \
    pip install --no-cache-dir Logbook && \
    pip install --no-cache-dir python_dateutil && \
    pip install --no-cache-dir py_mini_racer && \
    pip install --no-cache-dir tqdm && \
    pip install --no-cache-dir beautifulsoup4 && \
    pip install --no-cache-dir bokeh && \
    pip install --no-cache-dir pandas && \
    pip install --no-cache-dir tornado && \
    pip install --no-cache-dir mini-racer && \
    pip install --no-cache-dir easytrader

# 下载并编译TA-Lib（使用国内镜像加速）
RUN mkdir -p ta-lib && \
    # 使用多个镜像源尝试下载，提高成功率，并验证文件大小（有效的tar.gz应该大于100KB）
    (curl -SL --connect-timeout 10 --max-time 60 -L https://ghps.cc/https://github.com/TA-Lib/ta-lib/releases/download/v0.6.4/ta-lib-0.6.4-src.tar.gz -o /tmp/ta-lib.tar.gz && [ $(stat -c%s /tmp/ta-lib.tar.gz) -gt 100000 ] || \
     curl -SL --connect-timeout 10 --max-time 60 -L https://mirror.ghproxy.com/https://github.com/TA-Lib/ta-lib/releases/download/v0.6.4/ta-lib-0.6.4-src.tar.gz -o /tmp/ta-lib.tar.gz && [ $(stat -c%s /tmp/ta-lib.tar.gz) -gt 100000 ] || \
     curl -SL --connect-timeout 10 --max-time 60 -L https://github.com/TA-Lib/ta-lib/releases/download/v0.6.4/ta-lib-0.6.4-src.tar.gz -o /tmp/ta-lib.tar.gz && [ $(stat -c%s /tmp/ta-lib.tar.gz) -gt 100000 ] || \
     wget -O /tmp/ta-lib.tar.gz --timeout=60 --tries=3 https://github.com/TA-Lib/ta-lib/releases/download/v0.6.4/ta-lib-0.6.4-src.tar.gz && [ $(stat -c%s /tmp/ta-lib.tar.gz) -gt 100000 ]) && \
    tar -xz --strip-components=1 -C ./ta-lib -f /tmp/ta-lib.tar.gz && \
    cd ta-lib/ && \
    ./configure && \
    make && \
    make install && \
    cd .. && \
    ldconfig && \
    pip install --no-cache-dir TA-Lib && \
    rm -rf ta-lib* /tmp/ta-lib.tar.gz

# 清理构建工具和缓存（减小镜像体积）
RUN apt-get --purge remove -y gcc make python3-dev default-libmysqlclient-dev curl wget && \
    apt-get autoremove -y && \
    rm -rf /root/.cache/* && \
    rm -rf /var/lib/apt/lists/* && \
    apt-get clean && \
    apt-get autoclean

WORKDIR /data
#InStock软件
COPY . /data/InStock
COPY cron/cron.hourly /etc/cron.hourly
COPY cron/cron.workdayly /etc/cron.workdayly
COPY cron/cron.monthly /etc/cron.monthly

#add cron sesrvice.
#任务调度
RUN chmod 755 /data/InStock/instock/bin/run_*.sh && \
    chmod 755 /etc/cron.hourly/* && chmod 755 /etc/cron.workdayly/* && chmod 755 /etc/cron.monthly/* && \
    echo "SHELL=/bin/sh \n\
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin \n\
    # min hour day month weekday command \n\
    */30 9,10,11,13,14,15 * * 1-5 /bin/run-parts /etc/cron.hourly \n\
    30 17 * * 1-5 /bin/run-parts /etc/cron.workdayly \n\
    30 10 * * 3,6 /bin/run-parts /etc/cron.monthly \n" > /var/spool/cron/crontabs/root && \
    chmod 600 /var/spool/cron/crontabs/root

ENTRYPOINT ["supervisord","-n","-c","/data/InStock/supervisor/supervisord.conf"]